/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * kitchenpanel.java
 *
 * Created on Oct 11, 2010, 10:42:24 AM
 */

package cat20032010;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


import javax.swing.*;
import java.util.Date;
/**
 *
 * @author acer
 */
public class kitchenpanel extends javax.swing.JPanel {
    Timer timer = new Timer (1000, new TimerListener());
    Vector data = new Vector();
    Vector columnNames = new Vector();
    Statement stmt;
    private ResultSet rset;
    String[] temp;
    /** Creates new form kitchenpanel */
    public kitchenpanel() {
    	Main.getConnection();
        try {
			stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        initComponents();
        getdata();
        timer.start();

    }
public void getdata()
    {
	Date currentDate = new Date();
	SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
	String dateString = formatter.format(currentDate);
		data = new Vector();
        columnNames = new Vector();
        try {
            
            rset = stmt.executeQuery("SELECT TABLE_NUM AS TABLE_NUMBER ,MENU_CODE AS MENU_CODE, MENU_NAME AS MENU_NAME, MENU_QTY AS QUANTITY , KITCHEN_STATUS AS STATUS,TIME_ORDER AS TIME_ORDER FROM ORDER_LIST where Order_date = '"+dateString+"' and (kitchen_status = 'PENDING' or kitchen_status = 'INPROGRESS') ");
            rset.first();

            ResultSetMetaData md = rset.getMetaData();
            int columns = md.getColumnCount();

            //  Get column names

            for (int i = 1; i <= columns; i++) {
                columnNames.addElement(md.getColumnName(i));
            }

            rset.first();
            if (rset.isFirst() == true && rset.isLast() == true) {
            
            }
            else if (rset.isFirst() == true) {
                // JOptionPane.showMessageDialog(null, "Hello");
                Vector row = new Vector(columns);

                for (int i = 1; i <= columns; i++) {
                    row.addElement(rset.getString(i));
                }

                data.addElement(row);
            }else{
                System.out.print("sssssaaaaaaaaaaaaa");
               // JOptionPane.showMessageDialog(null, "No Record Found!", "MESSAGE!", JOptionPane.INFORMATION_MESSAGE);
            }


            if (rset.isFirst() == true && rset.isLast() == true) {
                // JOptionPane.showMessageDialog(null, "Hello");
                Vector row = new Vector(columns);

                for (int i = 1; i <= columns; i++) {
                    row.addElement(rset.getString(i));
                }

                data.addElement(row);
            } else {
                while (rset.next()) {
                    // rset.next();
                    Vector row = new Vector(columns);

                    for (int i = 1; i <= columns; i++) {
                        row.addElement(rset.getString(i));
                    }

                    data.addElement(row);
                }
            }
        }
            catch (SQLException e) {
                System.out.println("fail");
            }

        DefaultTableModel model = new DefaultTableModel(data, columnNames)
        
       {
        public boolean isCellEditable(int row,int column){
            return false;

        }
    };
        jTable1.setModel(model);


    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        quantity = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        menuname = new javax.swing.JTextField();

        setPreferredSize(new java.awt.Dimension(620, 460));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Kitchen Module", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 1, 24))); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(620, 460));
        jPanel1.setLayout(null);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "TABLE_NUMBER", "MENU_CODE", "MENU_NAME", "QUANTITY", "STATUS"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(20, 70, 584, 193);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText(":");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(160, 360, 21, 17);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/button/PROCEED1.png"))); // NOI18N
        jButton2.setBorderPainted(false);
        jButton2.setContentAreaFilled(false);
        jButton2.setDefaultCapable(false);
        jButton2.setFocusPainted(false);
        jButton2.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/button/PROCEED1MouseOver.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2);
        jButton2.setBounds(460, 350, 140, 68);

        quantity.setEditable(false);
        jPanel1.add(quantity);
        quantity.setBounds(190, 360, 122, 20);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setText("Quantity");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(60, 360, 100, 17);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel4.setForeground(new java.awt.Color(153, 0, 0));
        jLabel4.setText("Selected");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(30, 300, 80, 17);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setText("Menu Name");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(60, 330, 100, 17);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel7.setText(":");
        jPanel1.add(jLabel7);
        jLabel7.setBounds(160, 330, 21, 17);

        menuname.setEditable(false);
        jPanel1.add(menuname);
        menuname.setBounds(190, 330, 122, 20);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        kitchen_frame.kitchenpanel.setVisible(false);
        kitchen_frame.dishesupdatepanel.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (menuname.getText().equals(""))
            JOptionPane.showMessageDialog(null, "No Data Selected", "Fail", JOptionPane.ERROR_MESSAGE);
        else{
        JOptionPane.showMessageDialog(null, "Selected Menu is : "+temp[2]+" Quantity is : "+temp[3], "Success!", JOptionPane.INFORMATION_MESSAGE);
        kitchen_frame.kitchenpanel.setVisible(false);
        kitchen_frame.dishesupdatepanel.setVisible(true);
        menuname.setText("");
        quantity.setText("");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
  int column = jTable1.getSelectedColumn();
     int width = jTable1.getSelectedRow();
     if (jTable1.editCellAt(width, column, evt)){
         System.out.println("problem");
     }
     System.out.println("abc: "+data.indexOf(2, 1));
     String str1 = null;
     Vector tempdata = (Vector) data.get(width);
     str1 = tempdata.toString();


     str1 = removeCharAt(str1,0);

     str1 = removeCharAt(str1,str1.length()-1);

     kitchen_frame.str = str1;

     String str = kitchen_frame.str;

    temp = str.split("\\,");
    	for(int i =1; i < temp.length ; i++)
    	    temp[i]= removeCharAt(temp[i],0);
    	
    	for(int i =1; i < temp.length ; i++)
    		System.out.println(temp[i]);
    menuname.setText(temp[2]);
    quantity.setText(temp[3]);

     kitchen_frame.dishesupdatepanel.passing();
    }//GEN-LAST:event_jTable1MouseClicked
    
               
    public static String removeCharAt(String s, int pos) {
    	   StringBuffer buf = new StringBuffer( s.length() - 1 );
    	   buf.append( s.substring(0,pos) ).append( s.substring(pos+1) );
    	   return buf.toString();
    	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField menuname;
    private javax.swing.JTextField quantity;
    // End of variables declaration//GEN-END:variables

     private class TimerListener implements ActionListener{
	   public void actionPerformed(ActionEvent e){
		   getdata();
	   }
   }

}
